<template>
    <div class="inspection-results-view project-file-list-view">

      <modules-title class="title-position" titleName="检验结果"></modules-title>

      <div class="pj-content">

        <div class="screen-view">
          <el-form
            ref="queryForm"
            :model="screenData"
            :inline="true"
            label-width="68px"
          >

            <el-form-item label="项目">
              <el-select
                clearable
                v-model="screenData.project_id"
                placeholder="请选择项目"
                size="small"
                @change="projectChange"
              >
                <el-option
                  v-for="dict in projectOptions"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="报告来源">
              <el-select
                clearable
                v-model="screenData.if_wd"
                placeholder="请选择来源"
                size="small"
                @change="modelChange"
                @clear="modelClear"
              >
                <el-option
                  v-for="dict in modelOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="结果类型">
              <el-select
                clearable
                v-model="screenData.select_type"
                placeholder="请选择类型"
                size="small"
                @change="typeChange"
              >
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="关键字">
              <el-input v-model="screenData.select_str" placeholder="请输入关键字" clearable></el-input>
            </el-form-item>

            <el-form-item>

              <el-button type="primary" size="small" @click="handleQuery">搜索
              </el-button>
              <el-button plain size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-table
          @row-click="tableRowClick"
          size="medium"
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            align="center"

            label="文件名称">
            <template slot-scope="scope">
              <span>{{scope.row.old_name+scope.row.suffix}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="file_no"
            label="文件编号">
          </el-table-column>
          <el-table-column
            align="center"
            label="文件版本"
            prop="file_version">
          </el-table-column>

          <el-table-column
            align="center"
            label="检品名称"
            prop="sample_name">
          </el-table-column>
          <el-table-column
            align="center"
            label="批号"
            prop="batch_no">
          </el-table-column>

          <el-table-column
            align="center"
            prop="project_name"
            label="所属项目">
          </el-table-column>
          <el-table-column
            align="center"
            prop="creator_name"
            label="上传人员">
          </el-table-column>
          <el-table-column
            align="center"
            label="上传时间">
            <template slot-scope="scope">
              <span>{{scope.row.created_at ? dateFormat1('yyyy-mm-dd HH:MM:SS',scope.row.created_at) : ''}}</span>
            </template>
          </el-table-column>
<!--          <el-table-column-->
<!--            align="center"-->
<!--            prop="ipd_type_name"-->
<!--            label="文件类型">-->
<!--          </el-table-column>-->
          <el-table-column
            align="center"
            label="文件类型">
            <template slot-scope="scope">
              <template v-if="scope.row.ipd_type_name">
                <span >{{scope.row.ipd_type_name}}</span>
              </template>
              <template v-else>
                {{getSource(scope.row)}}
              </template>

            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="文件状态">
            <template slot-scope="scope">
              <span v-if="!scope.row.file_effect">草稿</span>
              <span v-else-if="scope.row.file_effect == 1">生效</span>
              <span v-else-if="scope.row.file_effect == 2">作废</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination style="display:flex;justify-content:flex-end;" :total="total" :page.sync="screenData.pager.skip"
                    :limit.sync="screenData.pager.limit" @pagination="getListData" />

      </div>

    </div>
</template>

<script>
    export default {
        name: "index",
        data:function () {
            return{
                tableData:[],
                total:0,
                screenData:{
                    project_id:'',
                    select_str:'',
                    select_type:'',
                    ipd_file_type:'',
                    if_wd:-1,
                    pager:{
                        skip:1,
                        limit:10,
                    }
                },
                projectOptions:[],
                typeOptions:[],
                modelOptions:[
                    {
                        label:'全部',
                        value:-1,
                    },
                    {
                        label:'检验任务',
                        value:0,
                    },
                    {
                        label:'稳定性实验',
                        value:1,
                    }
                ],
                childModel:{}

            }
        },
        created(){
            if (this.$store.getters.fileChildModel.length){
                this.childModel = this.$store.getters.fileChildModel
            }else {
                this.$store.dispatch('sys/getFileSource').then(()=>{
                    this.childModel = this.$store.getters.fileChildModel
                })
            }

            this.getProjectList();
            this.getListData();
            this.$api.PROJECT_START_FILE_TYPE().then(res=>{
                if (res.code == 200){
                    if (res.data && res.data.length){
                        res.data.forEach(item=>{
                            if (item.Key == 'inspection_task'){
                                // let data = {
                                //     value:item.Key,
                                //     label:item.Value
                                // }
                                // this.modelOptions.push(data)
                                if (item.Children && item.Children.length){
                                    item.Children.forEach(a=>{
                                        a.value = a.Key
                                        a.label = a.Value
                                        this.typeOptions.push(a)
                                    })
                                }
                            }
                        })
                    }
                }else{

                }
            })

            //this.typeOptions = this.$store.getters.sysDic('file_type')
        },
        methods:{
            getSource:function(row){
                if (row.model_children_type){
                    return  this.childModel[row.model_children_type]
                }else {
                    return  this.childModel[row.model_type]
                }
            },
            testChange:function(value){
                console.log(value)
                console.log(this.$refs.cascader.getCheckedNodes())
            },
            handleQuery:function(){
                this.screenData.pager.skip = 1;
                this.getListData()
            },
            resetQuery:function(){
                this.screenData = {
                    project_id:'',
                    select_str:'',
                    select_type:'',
                    ipd_file_type:'',
                    if_wd:-1,
                    pager:{
                        skip:1,
                        limit:10,
                    }
                }
                this.getListData()
            },
            getProjectList:function(){
                this.$api.PROJECT_START_PROJECT_ALL().then(res=>{
                    if (res.code === 200) {

                        if (res.data && res.data.data){
                            res.data.data.forEach(item=>{
                                let phases = ""
                                if (item.project_phases_id == "1"){
                                    phases = "临床前研究"
                                }else{
                                    phases = "临床研究"
                                }
                                item.name = item.project_name + '(' + phases + ')'
                            })
                            this.projectOptions = res.data.data || [];
                        }
                    }
                });
            },
            getListData:function () {

                this.$api.PROJECT_START_FILE_INSPECTION_LIST(this.screenData).then(res=>{
                    if (res.code == 200){
                        this.tableData = res.data.data
                        this.total = res.data.total
                    }else{
                        this.$message.error(res.msg)
                    }
                })
            },
            tableRowClick:function (row, column, event) {
                this.$router.push({name:'inspectionResults-file',query:{id:row.id}})
            },
            projectChange:function () {
                this.screenData.pager.skip = 1;
                this.getListData();
            },
            typeChange:function () {
                this.screenData.pager.skip = 1;
                this.getListData();
            },
            modelChange:function (value) {

                this.screenData.pager.skip = 1;
                this.getListData();
            },
            modelClear:function () {
                this.screenData.if_wd = -1
            }
        }
    }
</script>

<style lang="scss" scoped>
  @import "../../../../assets/scss/views/projectManagement/file/list.scss";
</style>
